#!/usr/bin/env python
# -*- coding: utf-8 -*-

from web.manager.oracle_manager import OracleManager
from web.models.macro_china_gdp import MacroChinaGdp


class MacroChinaGdpDao:
    """
    MacroChinaGdp的dao类
    """

    def __init__(self):
        self.db = OracleManager()

    def find_by_quarter(self, quarter) -> MacroChinaGdp:
        """
        根据quarter查询记录
        """

        self.db.connect()
        _list = self.db.execute_query(
            "select * from macro_china_gdp t where t.quarter='" + quarter + "'")
        self.db.close()
        if _list is not None and len(_list) > 0:
            macro_china_gdp = MacroChinaGdp()
            macro_china_gdp.id_ = _list[0][0]
            macro_china_gdp.quarter = _list[0][1]
            macro_china_gdp.gdp_av = _list[0][2]
            macro_china_gdp.gdp_yoyg = _list[0][3]
            macro_china_gdp.primary_industry_av = _list[0][4]
            macro_china_gdp.primary_industry_yoyg = _list[0][5]
            macro_china_gdp.secondary_industry_av = _list[0][6]
            macro_china_gdp.secondary_industry_yoyg = _list[0][7]
            macro_china_gdp.tertiary_industry_av = _list[0][8]
            macro_china_gdp.tertiary_industry_yoyg = _list[0][9]
            return macro_china_gdp
        else:
            return None

    def batch_insert(self, macro_china_gdp_list):
        """
        批量插入记录
        """

        sql = ('insert into macro_china_gdp(QUARTER, GDB_AV, GDB_YOYG, PRIMARY_INDUSTRY_AV, PRIMARY_INDUSTRY_YOYG, '
               'SECONDARY_INDUSTRY_AV, SECONDARY_INDUSTRY_YOYG, TERTIARY_INDUSTRY_AV, TERTIARY_INDUSTRY_YOYG) '
               'values(:QUARTER, :GDB_AV, :GDB_YOYG, :PRIMARY_INDUSTRY_AV, :PRIMARY_INDUSTRY_YOYG, '
               ':SECONDARY_INDUSTRY_AV, :SECONDARY_INDUSTRY_YOYG, :TERTIARY_INDUSTRY_AV, :TERTIARY_INDUSTRY_YOYG)')
        param_list = list()
        for macro_china_gdp in macro_china_gdp_list:
            param = (macro_china_gdp.quarter, macro_china_gdp.gdp_av, macro_china_gdp.gdp_yoyg, macro_china_gdp.primary_industry_av,
                     macro_china_gdp.primary_industry_yoyg, macro_china_gdp.secondary_industry_av, macro_china_gdp.secondary_industry_yoyg,
                     macro_china_gdp.tertiary_industry_av, macro_china_gdp.tertiary_industry_yoyg)
            param_list.append(param)
        self.db.connect()
        self.db.batch_insert(sql, param_list)
        self.db.close()